<template>
    <!-- pages/collectioncode/bindofficialaccount/index.wxml -->
    <view class="pages">
        <view class="code">长按识别二维码</view>
        <view class="hr"></view>
        <image class="bg_img" :src="shopimgUrl + 'bindofficialaccount_bg.png'"></image>
        <view class="wx_box">
            <view class="canvas-box">
                <canvas v-if="!canvasHidden" style="width: 400rpx; height: 400rpx" canvas-id="mycanvas" />
            </view>
            <!-- <web-view class="wx_img" src="{{imagePath}}"></web-view> -->
            <image class="wx_img" mode="scaleToFill" :src="imagePath" show-menu-by-longpress></image>
        </view>
        <view class="wx_msg">关注公众号开启支付提醒功能</view>
    </view>
</template>

<script>
// pages/collectioncode/bindofficialaccount/index.js
const app = getApp(); // 引入app

const QR = require('../../../utils/qrcode.js');

import _request from '../../../utils/request.js';
export default {
    data() {
        return {
            shopimgUrl: app.globalData.shopimgUrl,
            codeImg: '',
            canvasHidden: false,
            imagePath: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        // 公众号推送码
        _request
            .get(
                '/mp/getTicket',
                {
                    userId: uni.getStorageSync('userId')
                },
                app.globalData.yiyueType
            )
            .then((res) => {
                if (res.data.code == 200) {
                    this.setData({
                        codeImg: res.data.data.url
                    });
                    const size = this.setCanvasSize(); //动态设置画布大小

                    QR.api.draw(res.data.data.url, 'mycanvas', size.w, size.h);
                    setTimeout(() => {
                        this.canvasToTempImage();
                    }, 1000);
                }
            });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        /**
         * 点击图片进行预览
         */
        previewImg: function (e) {
            var img = this.imagePath;
            console.log(img);
            uni.previewImage({
                current: img,
                // 当前显示图片的http链接
                urls: [img] // 需要预览的图片http链接列表
            });
        },

        /**
         * 获取临时缓存照片路径，存入data中
         */
        canvasToTempImage: function () {
            const that = this; //把当前画布指定区域的内容导出生成指定大小的图片，并返回文件路径。

            uni.canvasToTempFilePath({
                canvasId: 'mycanvas',
                success: function (res) {
                    const tempFilePath = res.tempFilePath;
                    console.log(tempFilePath);
                    that.setData({
                        imagePath: tempFilePath // canvasHidden:true
                    });
                },
                fail: function (res) {
                    console.log(res);
                }
            });
        },

        //适配不同屏幕大小的canvas
        setCanvasSize: function () {
            var size = {};

            try {
                var res = uni.getSystemInfoSync();
                var scale = 750 / 400; //不同屏幕下canvas的适配比例；设计稿是750宽  686是因为样式wxss文件中设置的大小

                var width = res.windowWidth / scale;
                var height = width; //canvas画布为正方形

                size.w = width;
                size.h = height;
            } catch (e) {
                // Do something when catch error
                console.log('获取设备信息失败' + e);
            }

            return size;
        }
    }
};
</script>
<style>
/* pages/collectioncode/bindofficialaccount/index.wxss */
.pages {
    position: relative;
    margin: 30rpx;
    text-align: center;
}

.code {
    height: 56rpx;
    font-size: 56rpx;
    font-family: ZhenyanGB-Regular, ZhenyanGB;
    font-weight: 400;
    color: #ffffff;
    line-height: 56rpx;
    letter-spacing: 6px;
    text-shadow: 0px 4px 8px #da6b06;
    padding-top: 94rpx;
    z-index: 2;
}

.bg_img {
    position: absolute;
    left: 0;
    right: 0;
    width: 692rpx;
    height: 934rpx;
    top: 0;
    z-index: -1;
}

.hr {
    width: 666rpx;
    border: 2px dashed #ffffff;
    margin-top: 50rpx;
    height: 0;
    z-index: 2;
}

.wx_box {
    width: 400rpx;
    height: 400rpx;
    border-radius: 16px;
    /* border: 1px solid #FF8722; */
    margin: 0 auto;
    margin-top: 104rpx;
    z-index: 2;
}

.wx_img {
    width: 400rpx;
    height: 400rpx;
    z-index: 2;
}

.wx_msg {
    margin-top: 30rpx;
    height: 40rpx;
    font-size: 28rpx;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #ffffff;
    line-height: 40rpx;
    z-index: 2;
}

.canvas-box {
    position: fixed;
    top: 999999rpx;
    left: 0;
}
</style>
